package com.shop.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.mapper.AuctionproductCustomMapper;
import com.shop.mapper.AuctionproductMapper;
import com.shop.mapper.AuctionrecordMapper;
import com.shop.pojo.Auctionproduct;
import com.shop.pojo.AuctionproductExample;
import com.shop.pojo.AuctionproductExample.Criteria;
import com.shop.pojo.Auctionrecord;
import com.shop.pojo.Auctionuser;
import com.shop.pojo.CustomAuctionproduct;
import com.shop.service.AuctionService;
import com.shop.tools.CustomException;

@Service
public class AuctionServiceImpl implements AuctionService {
	
	@Autowired
	private AuctionproductMapper auctionproductMapper;
	@Autowired
	private AuctionproductCustomMapper auctionproductCustomMapper;
	@Autowired
	private AuctionrecordMapper auctionrecordMapper;

	@Override
	public List<Auctionproduct> findAllAuctions(Auctionproduct auction) {
		AuctionproductExample example = new AuctionproductExample();
		Criteria criteria = example.createCriteria();
		
		if(auction!=null) {
			if(auction.getAuctionname()!=null&&!"".equals(auction.getAuctionname())) {
				System.out.println(auction.getAuctionname());
				criteria.andAuctionnameLike("%"+auction.getAuctionname()+"%");
			}
			if(auction.getAuctiondesc()!=null&&!"".equals(auction.getAuctiondesc())) {
				criteria.andAuctiondescLike("%"+auction.getAuctiondesc()+"%");
			}
			if(auction.getAuctionstarttime()!=null) {
				criteria.andAuctionstarttimeGreaterThan(auction.getAuctionstarttime());
			}
			if(auction.getAuctionendtime()!=null) {
				criteria.andAuctionendtimeLessThan(auction.getAuctionendtime());
			}
			if(auction.getAuctionstartprice()!=null) {
				criteria.andAuctionstartpriceEqualTo(auction.getAuctionstartprice());
			}
		}
		example.setOrderByClause("auctionstarttime desc");
		List<Auctionproduct> auctionlist = auctionproductMapper.selectByExample(example);
		return auctionlist;
	}

	//查询拍卖品所有信息
	@Override
	public Auctionproduct selectAuctionInfo(Integer auctionId) {
		return auctionproductCustomMapper.selectAuctionInfo(auctionId);
	}

	//添加拍卖品
	@Override
	public int insertSelective(Auctionproduct record) {
		return auctionproductMapper.insertSelective(record);
	}

	//查询竞拍品
	@Override
	public Auctionproduct selectAuctionById(Integer id) {
		return auctionproductMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateAuction(Auctionproduct auction) {
		return auctionproductMapper.updateByPrimaryKeySelective(auction);
	}

	/**
	 * 插入竞拍记录
	 * 判断竞拍时间，竞拍价格
	 * @param record
	 * @return
	 * @throws CustomException 
	 */
	@Override
	public int insertAuctionRecord(Auctionrecord record) throws CustomException {
		//查询出当前竞拍品的所有信息
		Auctionproduct curAuction = auctionproductCustomMapper.selectAuctionInfo(record.getAuctionid());
		//判断竞拍时间
		if(!curAuction.getAuctionendtime().after(record.getAuctiontime())) {
			throw new CustomException("竞拍时间已经结束！");
		}
		//判断竞拍价格
		//当前竞拍品是否有竞拍记录，有，与最高竞拍价格比较；没有，与起拍价比较
		if(curAuction.getAuctionrecords().size()>0) {
			BigDecimal maxAuctionprice = curAuction.getAuctionrecords().get(0).getAuctionprice();
			if(record.getAuctionprice().compareTo(maxAuctionprice)<1) {
				throw new CustomException("竞拍价格不能低于当前竞拍的最高价！");
			}
		}else {
			if(record.getAuctionprice().compareTo(curAuction.getAuctionstartprice())<0) {
				throw new CustomException("竞拍价格不能低于起拍价！");
			}
		}
		return auctionrecordMapper.insertSelective(record);
	}

	@Override
	public List<CustomAuctionproduct> listAuctionEnd() {
		return auctionproductCustomMapper.selectAuctionEnd();
	}

	@Override
	public List<Auctionproduct> listAuctionNotEnd() {
		return auctionproductCustomMapper.selectAuctionNotEnd();
	}

	@Override
	public int deleteAuction(Integer id) {
		return auctionproductMapper.deleteByPrimaryKey(id);
	}

}
